package com.ruoyi.web.controller.purchase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.purchase.domain.*;
import com.ruoyi.purchase.mapper.PoPurchaseMapper;
import com.ruoyi.purchase.service.*;
import com.ruoyi.system.service.ISysUserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.junit.jupiter.params.shadow.com.univocity.parsers.annotations.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 采购单管理 Controller
 */
@Controller
@RequestMapping("/purchase/manage")
public class PurchaseManageController extends BaseController {
    private String prefix="purchase/manage/";

    @Autowired
    private PoPurchaseService poPurchaseService;
    @Autowired
    private PoBrandService poBrandService;
    @Autowired
    private PoTypeService poTypeService;
    @Autowired
    private PoProductService poProductService;
    @Autowired
    private PoFirmService poFirmService;
    @Autowired
    private PoDetailService poDetailService;
    @Autowired
    private ISysUserService iSysUserService;

    @RequiresPermissions("purchase:manage:view")
    @RequestMapping()
    public String purManage(){
        return prefix+"purManage";
    }

    @RequiresPermissions("purchase:manage:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(PoPurchase poPurchase){
        startPage();
        List<PoPurchase> list = poPurchaseService.findPurList(poPurchase);
        System.out.println(list);

        return getDataTable(list);
    }

    @RequestMapping("/add")
    public String add(Model model){
        List<PoBrand> brandList = poBrandService.list();
        model.addAttribute("brandList",brandList);
        List<PoType> typeList = poTypeService.list();
        model.addAttribute("typeList",typeList);
        List<PoProduct> proList = poProductService.list();
        model.addAttribute("proList",proList);
        List<PoFirm> firmList = poFirmService.findFirmList();
        model.addAttribute("firmList",firmList);
        model.addAttribute("pUser",getSysUser());

        return prefix+"add";
    }

    @RequiresPermissions("purchase:manage:add")
    @Log(title = "采购单信息",businessType = BusinessType.INSERT)
    @RequestMapping("/doAdd")
    @ResponseBody
    public AjaxResult addSave(@ModelAttribute("poPurchase") PoPurchase poPurchase,@ModelAttribute("poProduct") PoProduct poProduct,@ModelAttribute("poDetail") PoDetail poDetail){

            poDetail.setPoPurId(poPurchase.getPurId());
            poDetail.setPoProductId(poProduct.getProductId());

            return toAjax(poPurchaseService.save(poPurchase)&&poDetailService.save(poDetail));
    }

    //@RequiresPermissions("purchase:manage:edit")
    @RequestMapping("/edit/{purId}")
    public String edit(@PathVariable("purId") String purId,Model model){
        PoPurchase poPurchase = poPurchaseService.getById(purId);
        model.addAttribute("poPurchase",poPurchase);
        model.addAttribute("pUser",getSysUser());
        System.out.println(poPurchase);
        return prefix+"edit";
    }

    //@RequiresPermissions("purchase:manage:edit")
    @Log(title = "采购单修改", businessType = BusinessType.UPDATE)
    @RequestMapping("/doEdit")
    @ResponseBody
    public AjaxResult editSave(@Validated PoPurchase poPurchase){
        System.out.println(poPurchase);
        return toAjax(poPurchaseService.updateById(poPurchase));
    }

    @RequiresPermissions("purchase:manage:remove")
    @Log(title = "采购单删除", businessType = BusinessType.DELETE)
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        System.out.println(ids);
        return toAjax(poPurchaseService.removeById(ids));
    }

    @RequiresPermissions("purchase:manage:info")
    @RequestMapping("/info/{pruId}")
    public String info(@PathVariable("pruId") String purId,Model model){
        PoPurchase poPurchase = poPurchaseService.getById(purId);
        System.out.println(poPurchase);
        model.addAttribute("poPurchase",poPurchase);
        return prefix+"info";
    }

    @RequiresPermissions("purchase:manage:check")
    @RequestMapping("/check")
    @ResponseBody
    public AjaxResult check(PoPurchase poPurchase){
        System.out.println(poPurchase);
        return toAjax(poPurchaseService.updateStatus(poPurchase));
    }



}
